package com.qianniu.napi.admin.service.akka;

import akka.actor.UntypedActor;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.google.common.base.Strings;
import com.qianniu.napi.admin.entity.*;
import com.qianniu.napi.admin.mapper.LeagueMapper;
import com.qianniu.napi.admin.service.*;
import com.qianniu.napi.common.config.Constants;
import com.qianniu.napi.common.util.DateUtil;
import com.qianniu.napi.common.util.SpringUtil;
import com.qianniu.napi.common.util.WeixinUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * Created by chenxuebin on 2017/7/30.
 */
public class TemplatemsgActor extends UntypedActor {

    private final static Logger logger = LoggerFactory.getLogger(TemplatemsgActor.class);
    private IRedisService redisService = (IRedisService) SpringUtil.getBean("redisServiceImpl");
    private IOrderService orderService = (IOrderService) SpringUtil.getBean("orderServiceImpl");
    private ITemplatemsgService templatemsgService = (ITemplatemsgService) SpringUtil.getBean("templatemsgServiceImpl");

    @Override
    public void onReceive(Object message) {

        if (message instanceof String) {
            String msg = message.toString();
            logger.debug(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>> TemplatemsgActor receive message:" + msg);

            // 格式 type+ywid
            String[] arr = msg.split("@");
            if (arr.length != 3) {
                return;
            }

            String type = arr[0];
            String oid = arr[1];
            String tmplIds = arr[2];//上传时是PIC|acttype

            switch (type) {
                case "AddTplform":
                    logger.info(">>>>>>switch here[AddTplform]");
                    //type+ywid+acttype
                    addTplform(oid, tmplIds);
                    break;

                default:
                    break;
            }

            logger.debug(">>>>>> SignActor saveSign success");
        }
    }


    private void addTplform(String oid, String tmplIds) {

        logger.debug(">>>>>>>>>>>>>>>>>>>>>>>> addTplform ");
        Order order = orderService.selectById(oid);
        if (order == null) {
            logger.debug(">>>>>>>>>>>>>>>>>>>>>>>> addTplform order is null");
            return;
        }

        if (Strings.isNullOrEmpty(tmplIds)) {
            logger.debug(">>>>>>>>>>>>>>>>>>>>>>>> addTplform tmplIds is null");
            return;
        }

        User ruser = redisService.getUser(order.getUid());
        String[] tmplIdsArr = tmplIds.split(";");
        for (String item : tmplIdsArr) {
            Templatemsg tplmsg = templatemsgService.selectOne(new EntityWrapper<Templatemsg>()
                    .eq("tplid",item));
            if (tplmsg==null) {
                logger.debug(">>>>>>>>>>>>>>>>>>>>>>>> addTplform tplmsg is null for tplid="+item);
                continue;
            }

            Tplform tplform = new Tplform();
            tplform.setOpenid(ruser.getOpenid());
            tplform.setFormid(item);
            tplform.setFormname(tplmsg.getTplname());
            tplform.setState(1);

            tplform.setIsuse(0);
            tplform.setIsonce(tplmsg.getIsonce());
            tplform.setOrderno(order.getOrderNo());
            tplform.setMid(order.getMid());
            tplform.setMpid(order.getMpid());

            tplform.setFormdate(DateUtil.getToday());
            tplform.setCreatetime(new Date());
            tplform.setUid(order.getUid());

            tplform.setNickname(ruser.getNickName());
            tplform.setPhone(ruser.getPhone()==null?order.getPhone():ruser.getPhone());
            tplform.insert();

        }

    }

}
